<?php
/**
 * Created by PhpStorm.
 * User: august
 * Date: 2021/4/10
 * Time: 14:13
 */

use Aug\Tools\Design\Publisher;
use Aug\Tools\Design\Subscriber;

require __DIR__ . '/../vendor/autoload.php';

$pub = new Publisher("test");
$pub->setDataByKey("1", 'AAA1');
$pub->setDataByKey("2", 'AAA2');
$pub->setDataByKey("3", 'AAA3');
$pub->setDataByKey("4", 'AAA4');

$sub1 = new Subscriber("sub1", function(Publisher $pub){
    echo "sub1: " . $pub->getData()[1]. "\n";
}, 30);

$sub2 = new Subscriber("sub2", function(Publisher $pub){
    echo "sub2: " . $pub->getData()[2]. "\n";
}, 10);

$sub3 = new Subscriber("sub3", function(Publisher $pub){
    echo "sub3: " . $pub->getData()[3] . "\n";
}, 80);

$pub->attach($sub1);
$pub->attach($sub2);
$pub->attach($sub3);
echo "First:\n";
$pub->notify();

$sub4 = new Subscriber("sub4", function(Publisher $pub){
    echo "sub4: " . $pub->getData()[4]. "\n";
}, 6);
echo "\n Second:\n";
$pub->detach($sub3);
$pub->attach($sub4);
$pub->notify();